package com.deer.wms.base.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.dao.CellInfoMapper;
import com.deer.wms.base.system.model.Area.AreaInfo;
import com.deer.wms.base.system.model.Cell.CellInfo;
import com.deer.wms.base.system.model.Cell.CellInfoCriteria;
import com.deer.wms.base.system.model.Cell.CellInfoDto;
import com.deer.wms.base.system.model.Shelf.ShelfInfo;
import com.deer.wms.base.system.service.AreaInfoService;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.base.system.service.ShelfInfoService;
import com.deer.wms.busine.tactic.constant.BusineConstant;
import com.deer.wms.busine.tactic.model.Upper.UpperTacticDetailDto;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guo on 2019/11/27.
 */
@Service
@Transactional
public class CellInfoServiceImpl extends SuperServiceImpl<CellInfoMapper, CellInfo> implements CellInfoService {

    @Autowired
    private CellInfoMapper cellInfoMapper;

    @Autowired
    private CellInfoService cellInfoService;

    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private ShelfInfoService shelfInfoService;


    @Override
    public String getSysAcceptCell(Integer wareId) {
        if (ObjectUtil.isNull(wareId)) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "仓库ID为空");
        }
        CellInfo cellInfo = cellInfoMapper.getSysCell(wareId, 0);
        if (cellInfo == null) {
            AreaInfo areaInfo = new AreaInfo();
            areaInfo.setWareId(wareId);
            areaInfo.setAreaName(wareId + "0");
            areaInfo.setAreaCode(wareId + "0");
            areaInfo.setIsSys(1);
            areaInfoService.save(areaInfo);

            ShelfInfo shelfInfo = new ShelfInfo();
            shelfInfo.setAreaId(areaInfo.getAreaId());
            shelfInfo.setState(1);
            shelfInfo.setShelfCode(areaInfo.getAreaCode() + "0");
            shelfInfo.setShelfName(areaInfo.getAreaCode() + "0");
            shelfInfo.setIsSys(1);
            shelfInfo.setType(0);
            shelfInfoService.save(shelfInfo);

            cellInfo = new CellInfo();
            cellInfo.setShelfInfoId(shelfInfo.getShelfInfoId());
            cellInfo.setCellUseType(0);
            cellInfo.setCellCode(shelfInfo.getShelfCode() + "0");
            cellInfo.setCellName("内置验收默认货位");
            cellInfo.setIsSys(1);
            cellInfoService.save(cellInfo);

        }
        return cellInfo.getCellCode();
    }

    @Override
    public String getSysJhCell(Integer wareId) {
        CellInfo cellInfo = cellInfoMapper.getSysCell(wareId, 3);
        if (cellInfo == null) {
            AreaInfo areaInfo = new AreaInfo();
            areaInfo.setWareId(wareId);
            areaInfo.setAreaName(wareId + "3");
            areaInfo.setAreaCode(wareId + "3");
            areaInfo.setIsSys(1);
            areaInfoService.save(areaInfo);

            ShelfInfo shelfInfo = new ShelfInfo();
            shelfInfo.setAreaId(areaInfo.getAreaId());
            shelfInfo.setState(1);
            shelfInfo.setShelfCode(areaInfo.getAreaCode() + "3");
            shelfInfo.setShelfName(areaInfo.getAreaCode() + "3");
            shelfInfo.setIsSys(1);
            shelfInfo.setType(0);
            shelfInfoService.save(shelfInfo);

            cellInfo = new CellInfo();
            cellInfo.setShelfInfoId(shelfInfo.getShelfInfoId());
            cellInfo.setCellUseType(3);
            cellInfo.setCellCode(shelfInfo.getShelfCode() + "3");
            cellInfo.setCellName("内置集货默认货位");
            cellInfo.setIsSys(1);
            cellInfoService.save(cellInfo);

        }
        return cellInfo.getCellCode();
    }

    @Override
    public List<CellInfoDto> findList(CellInfoCriteria criteria) {
        return cellInfoMapper.findList(criteria);
    }

    @Override
    public List<CellInfoDto> findByWareIdList(CellInfoCriteria criteria) {
        return cellInfoMapper.findByWareIdList(criteria);
    }

    /**
     * 库位码查询仓库
     *
     * @param fromBoxCode
     * @return
     */
    @Override
    public Integer findbyCellCode(String fromBoxCode) {
        return baseMapper.findbyCellCode(fromBoxCode);
    }

    @Override
    public List<CellInfo> findByShelfInfoId(Integer shelfInfoId) {
        return cellInfoMapper.findByShelfInfoId(shelfInfoId);
    }

    @Override
    public void deleteByShelfId(Integer shelfInfoId) {
        cellInfoMapper.deleteByShelfId(shelfInfoId);
    }

    @Override
    public CellInfoDto findByCodeAndWareId(String itemCode, Integer wareId) {
        return cellInfoMapper.findByCodeAndWareId(itemCode, wareId);
    }

    @Override
    public void modifyCellState(String cellCode, Integer wareId, String sate, CurrentUser currentUser) {
        CellInfo cellInfo = cellInfoService.findByCodeAndWareId(cellCode, wareId);
        cellInfo.setState(sate);
        cellInfoService.updateById(cellInfo);
    }

    @Override
    public List<CellInfoDto> findByCellUseTypes(CellInfoCriteria criteria) {
        return cellInfoMapper.findByCellUseTypes(criteria);
    }

    @Override
    public String getNewInCell(Integer wareId) {
        String cellCode = cellInfoMapper.getNewInCell(wareId);

        return cellCode;
    }

    @Override
    public Integer getCellType(String cellCode, Integer wareId) {
        return cellInfoMapper.getCellType(cellCode, wareId);
    }

    @Override
    public CellInfo findBy(String field, String value) {
        QueryWrapper<CellInfo> qw = new QueryWrapper<>();
        return getOne(qw.eq(field, value));
    }

    @Override
    public List<CellInfoDto> getRecommendUpperCells(UpperTacticDetailDto upperTacticDetail) {
        List<CellInfoDto> recommendUpperCells = new ArrayList<>();
        CellInfoCriteria criteria = new CellInfoCriteria();
        //判断规则是否启用
        if (BusineConstant.TACTIC_SATE_INACTIVE.equals(upperTacticDetail.getState())) {
            return recommendUpperCells;
        }

        if (BusineConstant.UPPER_TACTIC_RULE_TO_AREA.equals(upperTacticDetail.getUpperTacticType())) {
            //按照库区推荐
            String toAreaCode = upperTacticDetail.getToAreaCode();
            criteria.setAreaCode(toAreaCode);
        } else if (BusineConstant.UPPER_TACTIC_RULE_TO_CELL.equals(upperTacticDetail.getUpperTacticType())) {
            //按照库位推荐
            String toCellCode = upperTacticDetail.getToCellCode();
            criteria.setCellCode(toCellCode);
        }
        recommendUpperCells = cellInfoService.findList(criteria);

        //FIXME:完善上架策略
//        Integer isEmptyCell = upperTacticDetail.getIsEmptyCell();
//        if(isEmptyCell==1){
//            cellInfoCriteria.setIsEmptyCell(1);
//        }
//        if(upperTacticDetail.getCellType()!=null && upperTacticDetail.getCellType() !=0){
//            cellInfoCriteria.setCellType(upperTacticDetail.getCellType());
//        }
//        if(upperTacticDetail.getCellUseType()!=null && upperTacticDetail.getCellUseType()!=0){
//            cellInfoCriteria.setCellUseType(upperTacticDetail.getCellUseType());
//        }
//        cellInfoCriteria.setState("enable");
//        if(upperTacticDetail.getCellAbcLevel()!=null && upperTacticDetail.getCellAbcLevel()!=0){
//            cellInfoCriteria.setCellAbcLevel(upperTacticDetail.getCellAbcLevel());
//        }

        return recommendUpperCells;
    }

  

}
